$(() => {
    const token = GetQueryString("token");
    let company_id = GetQueryString("company_id");
    let page = 1;
    let pageSize = 6;
    // 企业信息
    companyMsg();
    // 首次渲染商品页面
    company_goods();

    function companyMsg() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_detail', 'token': token, 'company_id': company_id },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let banner = '';

                    // 分享
                    let shareUrl = domain + "/view/share_companyDetail.html?company_id=" + data.id;
                    let shareTitle = data.company_name;
                    let shareDes = data.description;
                    let shareImgUrl = data.company_logo;

                    // 分享
                    $('.share').on('click', function() {
                        if (isAndroid_ios()) {
                            //安卓  
                            window.android.shareWebMethod(shareUrl, shareTitle, shareDes, shareImgUrl);
                        } else {
                            //ios
                            // window.webkit.messageHandlers.shareWebMethod.postMessage(shareUrl, shareTitle, shareDes, shareImgUrl);
                            var d = {};
                            d.shareUrl = shareUrl;
                            d.title = shareTitle;
                            d.description = shareDes;
                            d.imgUrl = shareImgUrl;
                            window.webkit.messageHandlers.shareWebMethod.postMessage(d);
                        }
                    })

                    // 标题
                    $('.hetitle').html(data.company_name);
                    // 是否收藏
                    $('#shopnati').attr('data-has_follow', data.has_follow);
                    if (data.has_follow == 1) {
                        $('#shopnati').addClass('on');
                    } else {
                        $('#shopnati').removeClass('on');
                    }
                    $('.select_company').attr('data-company_id', data.id)
                    // 联系客服
                    $('.contactBtn').attr('data-uid', data.hx_uid);
                    if (data.company_level == 0 || data.user_id == '') {
                        $('.contactService').hide();
                    }

                    // 企业logo
                    $('.company_logo').attr("src", data.company_logo == '' ? '../img/orimg.png' : data.company_logo);
                    $('.company_name').html(data.company_name);
                    if (data.level_logo != '') {
                        // 等级logo
                        $('.level_logo').attr('src', data.level_logo);
                    } else {
                        $('.level_logo').remove();
                    }
                    // 浏览量
                    $('.read_count').html('浏览量' + data.read_count);

                    data.pic.forEach(item => {
                        banner += `
                    <div class="swiper-slide"><img src="${item.pic_url == '' ? '../img/banner.png' : item.pic_url}" alt="${item.pic_name}" data-url="${item.pic_url}" class="carBanner baimg" /></div>
                    `;
                    })
                    $('.adBanner .swiper-wrapper').html(banner);
                    // banner轮播
                    if (data.pic.length == 1) {
                        new Swiper('.adBanner', {
                            autoplay: 3000,
                            // pagination: '.swiper-pagination',
                            loop: false
                        })
                    } else {
                        new Swiper('.adBanner', {
                            autoplay: 3000,
                            // pagination: '.swiper-pagination',
                            loop: true
                        })
                    }

                    // banner放大轮播
                    $('.carBanner').on('click', function() {
                        let idx = $(this).parent().index();
                        dialog('#plusBanner');
                        let html = '';
                        data.pic.forEach(item => {
                            html += `
                                <div class="swiper-slide" >
                                    <img class="showImg" src="${item.pic_url}" alt="">
                                </div>
                            `;
                        })
                        $('.plusBannerSwiper .swiper-wrapper').html(html);
                        let plusBanner = new Swiper('.plusBannerSwiper', {
                            pagination: '.swiper-pagination',
                            paginationType: 'fraction'
                        })
                        plusBanner.slideTo(idx, false);
                    })


                    // 图片放大轮播
                    // $('.plusBanner').on('click', function(){
                    $(document).on('click', '.plusBanner', function(e) {
                        let idx = $(this).parent().index();
                        var that = this;
                        // let idx = e.target.dataset.idx;
                        let img = $(that).attr('data-url');
                        let arr = [];
                        $('.showImg2').attr('src', img);
                        dialog('#plusBanner');
                        for (var i = 0; i < $(that).parent().parent().find("a").length; i++) {
                            arr.push($(that).parent().parent().find("img").eq(i).attr("data-url"));
                        }
                        let html = '';
                        arr.forEach(item => {
                            html += `
                                <div class="swiper-slide" style="height:5rem;">
                                    <img class="showImg2" style="width:100%;" src="${item}" alt="">
                                </div>
                            `;
                        })
                        $('.plusBannerSwiper>.swiper-wrapper').html(html);
                        // banner轮播
                        let plusBanner2 = new Swiper('.swiper-container.plusBannerSwiper', {
                            pagination: '.swiper-pagination',
                            paginationType: 'fraction',
                        })
                        if (idx == 0) {
                            $(".swiper-wrapper").css("transform", "translate3d(0px,0px,0px)")
                        }
                        plusBanner2.slideTo(idx,100,true);
                    })
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    //收藏企业
    $("#shopnati").on('click', function() {
        let has_follow = $('#shopnati').attr('data-has_follow');
        // console.log(has_follow);
        if (has_follow == 1) {
            $('#shopnati').removeClass('on');
        } else {
            $('#shopnati').addClass('on');
        }
        let company_id = $('.select_company').attr('data-company_id');
        let _this = $(this);
        // // console.log(company_id);
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|add_fav', 'token': token, 'company_id': company_id },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    let data = res.data;
                    $(_this).attr('data-has_follow', data.has_follow);
                    // companyMsg();
                    // alert(res.message);
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })

    // 商品
    function company_goods(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_goods', 'company_id': company_id, 'page': page, 'pagelimit': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.forEach(item => {
                        html += `
                        <li class="iz2item">
                            <a href="comp_shopDetail.html?id=${item.goods_id}&token=${token}" class="iz2link" >
                                <img src="${item.goods_thumb == '' ? '../img/banner.png' : item.goods_thumb}" alt="${item.goods_name}" class="iz2pic" />
                                <span class="iz2title">
                                    <p class="iz2ti1">${item.goods_name}</p>`
                        if (item.price_show_model == 1) {
                            html += `<p class="iz2ti2">¥${item.goods_price}</p>`
                        } else if (item.price_show_model == 2) {
                            html += `<p class="iz2ti2"><del style="color:#666;font-size:.24rem;">¥${item.price_before}</del>/<span style="font-size:.28rem;">¥${item.goods_price}</span></p>`
                        } else {
                            html += `<p class="iz2ti2">¥${item.price_area}</p>`
                        }
                        html += `</span>
                            </a>
                        </li>
                        `;
                    })
                    if (data.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.shopDetails').append(html);
                }
            }
        })
    }

    //相册
    function company_picture(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_goodpicture', 'company_id': company_id, 'page': page, 'pagelimit': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    data.forEach((item, idx) => {
                        html += `
                        <a href="javascript:;" class="shde_link">
                            <img src="${item.thumb_url == '' ? '../img/banner.png' : item.thumb_url}" alt="${item.pic_name}" data-url="${item.pic_url}" data-idx="${idx}" class="shde_pic plusBanner" />${item.pic_name}
                        </a>
                        `;
                    })
                    if (data.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.pictureDetails').append(html);
                }
            }
        })
    }

    //联系
    function company_contact() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_detail', 'company_id': company_id, 'token': token },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';
                    let phones = data.company_phones.split(',');
                    let addr = data.address.split(',');
                    addr.splice(0, 1, data.area_name + addr[0]);
                    html += `
                    <div class="shdetd">
                        <p class="shdetdt1">简介</p>
                        <p class="shdetdt2 hi" id="shdetdt2">${data.description}</p>
                        <a href="javascript:;" class="shdetdt3"><span class="shdetdt3link">更多</span><span class="shdetde3pic"></span></a>
                    </div>
                    <div class="shdetd">
                        <p class="shdetdt1">主营</p>
                        <p class="shdetdt4">${data.main_business}</p>
                    </div>
                    <div class="shdetd">
                        <p class="shdetdt1">电话</p>`
                    phones.forEach(item => {
                        html += `<a href="tel:${item}" class="shdetdt2 myPhone">手机：${item}</a>`
                    })
                    html += `</div>
                    <div class="shdetd">
                        <p class="shdetdt1">地址</p>`
                    addr.forEach(ad => {
                        html += `
                            <p class="shdetdt2">
                                ${ad}
                            </p>`
                    })
                    html += `</div>
                    `;
                    $('.contactDetails').html(html);
                    let he = $('#shdetdt2').height();
                    if (he < 30) {
                        $('.shdetdt3').hide();
                    }
                } else if (res.status == 2) {

                } else {
                    alert(res.message);
                }
            }
        })
    }

    // 建材圈
    function company_circle(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|company_forum', 'company_id': company_id, 'token': token, 'page': page, 'pagelimit': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let data = res.data;
                    let html = '';

                    data.forEach(item => {
                        let date = getDate(item.create_time);
                        html += `
                        <div class="ringitem">
                            <div class="ringtop" style="display:flex;align-items:center;">
                                <img src="${item.head_logo == '' ? '../img/orimg.png' : item.head_logo}" alt="${item.name}" class="ringtpic" />
                                <div class="ringtco" style="flex:1;">
                                    <div class="ringtct1">
                                        ${item.name}
                                        <p class="ringtlr">${date}</p>
                                    </div>`
                        if (item.level_logo != '') {
                            html += `<img src="${item.level_logo}" class="userLevel" alt="${item.level_name}">`
                        }
                        html += `</div>
                            </div>
                            <p class="ringco">${item.content}</p>
                            <p class="slh">...</p>
                            <a href="javascript:;" class="ringco2">全文</a>
                            <div class="ringlilist" data-id="${item.id}">`
                        item.pictures.forEach((i, idx) => {
                            html += `<a href="javascript:;"><img src="${i.thumb_url == '' ? '../img/banner.png' : i.thumb_url}" alt="${i.pic_name}" data-url="${i.pic_url}" data-idx="${idx}" class="rilipic plusBanner" /></a>`;
                        })
                        html += `</div>
                            <div class="browse">
                                <p class="brot">${item.read_count}人浏览</p>
                                <div class="brolr">
                                    <div class="brolrlink" deta-num="1">
                                        <span class="brolrpic brolrpic2"></span><span class="num">${item.digg_count}</span>
                                    </div>
                                </div>
                            </div>`
                        if (item.comments == '') {
                            html += '';
                        } else {
                            html += `<div class="brocomment">
                                <img src="../img/brotop.png" alt="" class="brotop" /><div class="commentLists">`

                            item.comments.forEach(p => {
                                html += `
                                <p class="brocoti"><a href="javascript:;" class="brocotlink">${p.nick_name}:</a>${p.content}</p>
                            `
                            })
                            html += `</div></div>`;
                        }
                        html += `</div>`;
                    })
                    if (data.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.circleDetails').append(html);

                    // 文本溢出显示全文按钮
                    for (var i = 0; i < $(".ringco").length; i++) {
                        let height = $(".ringco").eq(i).height(); //div的宽度
                        let scrollHeight = $(".ringco").eq(i)[0].scrollHeight; //文本的宽度
                        if (scrollHeight > height) {
                            $('.ringco2').eq(i).show();
                            $('.slh').eq(i).show();
                        } else {
                            $('.ringco2').hide();
                            $('.slh').hide();
                        }
                    }
                    // 点击全文显示
                    $('.ringco2').on('click', function() {
                        $(this).parent().find(".ringco").css('height', 'auto');
                        $(this).parent().find('.slh').hide();
                        $(this).hide();
                    })
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    $('.goodsTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 1);
        $('.shopDetails').html('');
        $('.notMore').css('display', 'none');
        company_goods(page);
    })
    $('.pictureTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 2);
        $('.pictureDetails').html('');
        $('.notMore').css('display', 'none');
        company_picture(page);
    })
    $('.contactTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 3);
        $('.notMore').css('display', 'none');
        company_contact();
    })
    $('.circleTab').on('click', function() {
        page = 1;
        $('.sh_delsitd').attr('data-type', 4);
        $('.circleDetails').html('');
        $('.notMore').css('display', 'none');
        company_circle(page);
    })

    // 拨号短信通知
    $(document).on('click', '.myPhone', function() {
        let phone = $(this).html();
        $.ajax({
            type: "post",
            url: url,
            data: { "method": "Api|call_msg_send", "token": token, "phone": phone },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    console.log(res.message);
                } else {
                    alert(res.message);
                }
            }
        })
    })

    // 联系客服
    $('.contactBtn').on('click', function() {
        let uid = $(this).attr('data-uid');
        if (uid != '') {
            if (isAndroid_ios()) {
                //安卓  
                window.android.startChat(uid);
            } else {
                //ios
                window.webkit.messageHandlers.startChat.postMessage(uid);
            }
        } else {
            alert('暂无客服信息！！！')
        }
    })

    // 下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                let type = $('.sh_delsitd').attr('data-type');
                if (type == 1) {
                    company_goods(page);
                } else if (type == 2) {
                    company_picture(page);
                } else if (type == 4) {
                    company_circle(page);
                }
            }
        }
    })
})